<?php
/**
 * Description of PresentacionP
 *
 * @author Nelifer
 */
include_once('BDManager.class.php');
require('Inventario.class.php');

class SalidaEntradaProducto {
    var $con;
    function SalidaEntradaProducto(){
        $this->con=new BDManager();
    }

    public function insertarSalida($campos){
        $inv = new Inventario();
        $cantInv = $inv->getCantidad($campos[0]);
        $cantSal = $campos[1] * -1;
        $cantInv = $cantInv + $cantSal ;
        if($this->con->conectar()==true){
            $sql = "UPDATE inventarios SET inv_canttotal = $cantInv WHERE inv_id = $campos[0] ";         
            $res1 = mysql_query($sql);
        }
        $res2 = $inv->insertarRegistroInv($campos[0], $cantSal, $campos[2], $campos[3],  $campos[4]);
        return ($res1==1 && $res2==1) ? true : false;
    }
    public function insertarEntrada($campos){
        $inv = new Inventario();
        $invId = $inv->insertarInventario(2, $campos[0], $campos[1]);
        if($invId != -1){
            $res1 = $inv->insertarRegistroInv($invId, $campos[1], $campos[2], $campos[3], $campos[4]);
        }   
     
        return ($res1==1) ? true : false;
    }    

    private function countRec($fname,$tname,$where) {
        $con = null;
        $sql = "SELECT count($fname) FROM $tname $where";
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $result = mysql_query($sql);
        }
        while ($row = mysql_fetch_array($result)) {
            return $row[0];
        }
    }
    
    public function consultar_grid($page=null, $rp=null, $sortname=null, $sortorder=null, $query=null, $letter_pressed=null, $qtype=null, $tipoProducto=null){
        if (!$sortname) $sortname = 'name';
        if (!$sortorder) $sortorder = 'desc';
        if($query!=''){
             $where = "WHERE `".$qtype."` LIKE '%".$query."%' ";
        } else {
            $where ='';
        }
        if($letter_pressed!=''){
            $where = "WHERE `".$qtype."` LIKE '".$letter_pressed."%' ";	
        }
        if($letter_pressed=='#'){
            $where = "WHERE `".$qtype."` REGEXP '[[:digit:]]' ";
        }
        $sort = "ORDER BY $sortname $sortorder";

        if (!$page) $page = 1;
        if (!$rp) $rp = 10;
        $start = (($page-1) * $rp);
        $limit = "LIMIT $start, $rp";
        $con = $this->con->conectar();
        if($tipoProducto==1){
            $sql = "SELECT inv.inv_id, inv.inv_canttotal, map.map_id , map.map_codigo, map.map_nombre, pre.pre_nombre ".
                    "FROM inventarios inv " .
                        "INNER JOIN materiasprimas map on map.map_id = inv.map_id " .
                        "INNER JOIN presentacionesproductos pre on pre.pre_id = map.pre_id  $where $sort $limit";            
            $total = $this->countRec('inv_id','inventarios',$where);
        }else if($tipoProducto==2){
            $sql = "SELECT CASE WHEN inv.inv_id IS NULL THEN 0 ELSE inv.inv_id END inv_id, ".
                        "CASE WHEN inv.inv_canttotal IS NULL THEN 0 ELSE inv.inv_canttotal END inv_canttotal, ".
                        "pte.pte_id, pte.pte_codigo, pte.pte_nombre, pre.pre_nombre ".
                    "FROM productosterminados pte " .
                        "LEFT JOIN inventarios inv on pte.pte_id = inv.pte_id AND inv.inv_tipo = 2 " .
                        "INNER JOIN presentacionesproductos pre on pre.pre_id = pte.pre_id  $where $sort $limit";           
            $total = $this->countRec('pte_id','productosterminados',$where);
        }
        $result = mysql_query($sql);
        //$this->con->close($con);
        // Return JSON data
        $data = array();
        $data['page'] = $page;
        $data['total'] = $total;
        $data['rows'] = array();
        if($tipoProducto==1){
            while ($row = mysql_fetch_assoc($result)) {
                $data['rows'][] = array(
                'id' => $row['inv_id'],
                'cell' => array($row['inv_id'], $row['map_codigo'], $row['map_nombre'], $row['pre_nombre'], $row['inv_canttotal'], )
                );
            }           
        }else if($tipoProducto==2){
            while ($row = mysql_fetch_assoc($result)) {
                $data['rows'][] = array(
                'id' => $row['pte_id'],
                'cell' => array($row['pte_id'], $row['pte_codigo'], $row['pte_nombre'], $row['pre_nombre'], $row['inv_canttotal'], )
                );
            }           
            
        }
        echo json_encode($data);
    }
}

?>
